package com.ocom.payconfig.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfDeviceInfo;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.entity.payconfig.EntityMqttDevice;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.pojo.payconfig.MachPriceVo;
import com.ocom.common.pojo.payconfig.MqttDeviceListVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.DishesPriceService;
import com.ocom.payconfig.service.MqttDeviceService;
import com.ocom.payconfig.service.XfDeviceInfoService;
import com.ocom.payconfig.service.XfPointInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
public class MqttController {


    @Autowired
    private MqttDeviceService mqttDeviceService;

    @Autowired
    private XfDeviceInfoService xfDeviceInfoService;

    @Autowired
    private XfPointInfoService xfPointInfoService;

    @Autowired
    private DishesPriceService dishesPriceService;


    /**
     * @param companyId 单位
     * @param deviceNo  Mqtt机器号
     * @return
     */
    @PostMapping(PayConfigUrl.URL_getMqttDeviceList)
    public Result<MqttDeviceListVo> getMqttDeviceList(Long companyId, String deviceNo) {
        EntityXfDeviceInfo entityXfDeviceInfo = xfDeviceInfoService.GetDeviceInfo(companyId, deviceNo);
        if (entityXfDeviceInfo == null) {
            return ResultUtil.error(ResultEnum.Err_2023);
        }

        List<EntityXfPointInfo> list = xfPointInfoService.getAllChildPoint(companyId, entityXfDeviceInfo.getPointId());
        if (list == null || list.size() == 0) {
            return ResultUtil.error(ResultEnum.Err_4075);
        }
        List<Integer> points = list.stream().map(EntityXfPointInfo -> EntityXfPointInfo.getPointId()).collect(Collectors.toList());

        List<EntityMqttDevice> entityMqttDevices = mqttDeviceService.getDeviceListByPoint(companyId, points);

        if (entityMqttDevices == null || entityMqttDevices.size() == 0) {
            return ResultUtil.error(ResultEnum.Err_3401);
        }

        List<Integer> calssIds = entityMqttDevices.stream().map(EntityMqttDevice -> EntityMqttDevice.getDishesClassId()).collect(Collectors.toList());


        List<MachPriceVo> machPriceVos = dishesPriceService.getMqttPrices(companyId, calssIds);

        List<MqttDeviceListVo> mqttDeviceListVo = new ArrayList<>();


        for (EntityMqttDevice entityMqttDevice:entityMqttDevices){

            MqttDeviceListVo mqttDeviceListVo1 = JSON.parseObject(JSON.toJSONString(entityMqttDevice),MqttDeviceListVo.class);

            for (MachPriceVo machPriceVo:machPriceVos){
                if(machPriceVo.getPrice()==null ) continue;
                List<Integer> dishesId = mqttDeviceListVo1.getDisheList().stream().map(EntityDishesInfo -> EntityDishesInfo.getDishesId()).collect(Collectors.toList());

                if(machPriceVo.getDishesClassId().equals(mqttDeviceListVo1.getDishesClassId())){

                    if(dishesId.contains(machPriceVo.getDishesId())){
                        LoopStop:
                        for (int i = 0; i < mqttDeviceListVo1.getDisheList().size(); i++) {
                            if(mqttDeviceListVo1.getDisheList().get(i).getDishesId().equals(machPriceVo.getDishesId())){
                                MqttDeviceListVo.MachPriceModeVo priceModeVo = new MqttDeviceListVo.MachPriceModeVo();
                                priceModeVo.setPrice(machPriceVo.getPrice());
                                priceModeVo.setPriceMode(machPriceVo.getPriceMode());
                                mqttDeviceListVo1.getDisheList().get(i).getPriceList().add(priceModeVo);
                            }
                        }
                    }else{
                        MqttDeviceListVo.MqttDeviceListVoInnVo deviceListVoInnVo = JSON.parseObject(JSON.toJSONString(machPriceVo), MqttDeviceListVo.MqttDeviceListVoInnVo.class);;
                        MqttDeviceListVo.MachPriceModeVo priceModeVo = new MqttDeviceListVo.MachPriceModeVo();
                        priceModeVo.setPrice(machPriceVo.getPrice());
                        priceModeVo.setPriceMode(machPriceVo.getPriceMode());
                        deviceListVoInnVo.getPriceList().add(priceModeVo);
                        mqttDeviceListVo1.getDisheList().add(deviceListVoInnVo);
                    }
                }

            }
            if(mqttDeviceListVo1.getDisheList().size()==0) mqttDeviceListVo1.setStatus(1);
            mqttDeviceListVo.add(mqttDeviceListVo1) ;
        }

        return ResultUtil.success(mqttDeviceListVo);

    }


    @PostMapping(PayConfigUrl.URL_getMqttDeviceInfo)
    public Result<EntityMqttDevice> getMqttDeviceInfo(String deviceNo) {
        EntityMqttDevice entityMqttDevice1 = mqttDeviceService.getDeviceListByDeviceNo(100l, deviceNo);
        EntityMqttDevice entityMqttDevice2 = mqttDeviceService.getDeviceListByDeviceNo(600l, deviceNo);

        EntityMqttDevice entityMqttDevice3 = null;
        if(entityMqttDevice1 ==null) entityMqttDevice3 =entityMqttDevice2;
        if(entityMqttDevice2 ==null) entityMqttDevice3 =entityMqttDevice1;

        if(entityMqttDevice1!=null && entityMqttDevice2!=null){
            if(ObjectUtil.isNotEmpty(entityMqttDevice1.getUpdateTime()) && ObjectUtil.isNotEmpty(entityMqttDevice2.getUpdateTime()) && entityMqttDevice1.getUpdateTime().compareTo(entityMqttDevice2.getUpdateTime())>=0){
                entityMqttDevice3 = entityMqttDevice1;
            }else{
                entityMqttDevice3 = entityMqttDevice2;
            }
        }
        if (entityMqttDevice3 == null) {
            return ResultUtil.error(ResultEnum.Err_3401);
        }
        return ResultUtil.success(entityMqttDevice3);
    }


    @PostMapping(PayConfigUrl.URL_updateMqttDevice)
    public Result updateByDeviceNo(@RequestBody EntityMqttDevice entityMqttDevice) {
        mqttDeviceService.updateByDeviceNo(entityMqttDevice.getComId(), entityMqttDevice);
        return ResultUtil.success();
    }


    @PostMapping(PayConfigUrl.URL_updateMqttDeviceOnline)
    public Result updateMqttDeviceOnline(@RequestBody List<String> devices) {
        mqttDeviceService.updateByDeviceNoOnlineState(100l,devices);
        mqttDeviceService.updateByDeviceNoOnlineState(600l,devices);
        return ResultUtil.success();
    }



}
